import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class GUI extends JPanel {

    private JLayeredPane layeredPane;
    private int state = 0;
    private JPanel sidebar;
    
    private final static int INFO = 1;
    private final static int INVENTAR = 2;
    private final static int OPTIONS = 3;

    public GUI()    {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(1024, 600));
        
        drawComponents();
        
        add(layeredPane);
    }
    
    private void setWindow() {
        JFrame frame = new JFrame("Bounty");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        JComponent newContentPane = new GUI();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setVisible(true);
    }

    private void drawComponents() {
    	
    	GUIComponent map = new GUIComponent(GUIComponent.MAP);
        map.setOpaque(true);
        map.setBounds(0, 0, 1024, 600);
        
        layeredPane.add(map, new Integer(0));
        
        GUIComponent menubar = new GUIComponent(GUIComponent.MENUBAR);
        menubar.setOpaque(true);
        menubar.setBounds(0, 557, 1024, 43);
        
        layeredPane.add(menubar, new Integer(1));
        
        GUIComponent timeWrap = new GUIComponent(GUIComponent.TIMEWRAP);
        timeWrap.setOpaque(true);
        timeWrap.setBounds(372, -30, 280, 82);
        
        layeredPane.add(timeWrap, new Integer(1));
        
        JTextField chatbar = new JTextField();
        chatbar.setOpaque(true);
        chatbar.setBounds(2, 559, 624, 39);
        chatbar.setBorder(BorderFactory.createLineBorder(Color.decode("#602f02"), 2));
        chatbar.setBackground(Color.decode("#cba675"));
        
        layeredPane.add(chatbar, new Integer(2));
        
        JButton send = new JButton("Senden");
        send.setOpaque(true);
        send.setBounds(631, 559, 100, 39);
        send.setBorder(BorderFactory.createLineBorder(Color.decode("#602f02"), 2));
        send.setBackground(Color.decode("#cba675"));
        send.setFocusPainted(false);
        send.setCursor(new Cursor(Cursor.HAND_CURSOR));
        
        layeredPane.add(send, new Integer(2));
        
        JTextArea chatLog = new JTextArea(5, 20);
        chatLog.setBounds(4, 454, 620, 100);
        chatLog.setEditable(false);
        chatLog.setVisible(false);
        
        layeredPane.add(chatLog, new Integer(2));
        
        JButton info = new JButton();
        info.setBounds(889, 559, 40, 39);
        info.setIcon(getIcon("img/buttons/information.png"));
        info.setBorder(BorderFactory.createLineBorder(Color.decode("#602f02"), 2));
        info.setBackground(Color.decode("#cba675"));
        info.setFocusPainted(false);
        info.setCursor(new Cursor(Cursor.HAND_CURSOR));
        info.addMouseListener(new menuMouseListener(this, menuMouseListener.CONVERSATION));
        
        layeredPane.add(info, new Integer(2));
        
        JButton inventar = new JButton();
        inventar.setBounds(934, 559, 40, 39);
        inventar.setIcon(getIcon("img/buttons/inventar.png"));
        inventar.setBorder(BorderFactory.createLineBorder(Color.decode("#602f02"), 2));
        inventar.setBackground(Color.decode("#cba675"));
        inventar.setFocusPainted(false);
        inventar.setCursor(new Cursor(Cursor.HAND_CURSOR));
        inventar.addMouseListener(new menuMouseListener(this, menuMouseListener.INVENTAR));
        
        layeredPane.add(inventar, new Integer(2));
        
        JButton options = new JButton();
        options.setBounds(979, 559, 40, 39);
        options.setIcon(getIcon("img/buttons/options.png"));
        options.setBorder(BorderFactory.createLineBorder(Color.decode("#602f02"), 2));
        options.setBackground(Color.decode("#cba675"));
        options.setFocusPainted(false);
        options.setCursor(new Cursor(Cursor.HAND_CURSOR));
        options.addMouseListener(new menuMouseListener(this, menuMouseListener.OPTIONS));
        
        layeredPane.add(options, new Integer(2));
        
        sidebar = new JPanel();
        drawSidebar();
        
        layeredPane.add(sidebar, new Integer(1));
        
        StartMenu startMenu = new StartMenu();
        JPanel sm1 = startMenu.drawContent();
        
        layeredPane.add(sm1, new Integer(999));
        
    }
    
    public void drawSidebar() {
    	
    	sidebar.setBounds(754, 0, 270, 557);
    	sidebar.setBackground(Color.decode("#cba675"));
    	
    	switch(state) {
        	case INFO:
        		JLabel conversationMenu = new JLabel("Haendlerinfo");
        		sidebar.add(conversationMenu);
        		sidebar.setVisible(true);
                break;
        	case INVENTAR:
        		JLabel inventory = new JLabel("Inventar");
        		sidebar.add(inventory);
        		sidebar.setVisible(true);
                break;
        	case OPTIONS:
        		JLabel optionsMenu = new JLabel("Optionen");
        		sidebar.add(optionsMenu);
        		sidebar.setVisible(true);
                break;
        	default:
        		sidebar.setVisible(false);
        }
    	
    }
    
    public void redrawSidebar() {
    	sidebar.setVisible(false);
    	sidebar.removeAll();
		drawSidebar();
    }
    
    public ImageIcon getIcon(String url) {
    	
    	try {
    		
    		Image img = ImageIO.read(new File(url));
    		return new ImageIcon(img);
    		
    	} catch (IOException ex) {
    		
    		System.out.println("Cannot read image!");
    		return null;
    		
    	}
    	
    }
    
    public void setState(int stateToChange) {
    	if(stateToChange == state)
    		state = 0;
    	else
    		state = stateToChange;
    	
    	redrawSidebar();
    }
    
    public int getState() {
    	return state;
    }

    public static void main(String[] args) {
    	
    	GUI gui = new GUI();
    	gui.setWindow();
    	
    }
}